﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using UserDefineForm.control;

namespace UserDefineForm.formcomponent
{
    public partial class DesignPanel : UserControl
    {
        private Modes mode= Modes.Design;
        private Control selectedControl = null;
        
            
        public Modes Mode
        {
            get { 
                return mode; 
            }
            set 
            {
                mode = value; 
                foreach (Control c in this.designPnl.Controls)
                {
                    if (c is DesignableControlBase)
                    {
                        ((DesignableControlBase)c).Mode = value;
                        this.Refresh();
                    }
                }
            }
        }

        public DesignPanel()
        {
            
            InitializeComponent();
            designPnl.Paint += new PaintEventHandler(DesignPanel_Paint);
        }

        void DesignPanel_Paint(object sender, PaintEventArgs e)
        {
            if ( Mode== Modes.Run || selectedControl == null) return;
            Pen dotPen = new Pen(Brushes.Black);
            dotPen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
            designPnl.CreateGraphics().DrawRectangle(dotPen,
                    selectedControl.Left-1,
                    selectedControl.Top-1,
                    selectedControl.Width +1,
                    selectedControl.Height +1);
        }

        public void setBackGround(Image img)
        {
            designPnl.BackgroundImage = img;
            if (img == null) return;
            designPnl.Size = img.Size;
            this.Size = img.Size;
            
            designPnl.BackgroundImageLayout = ImageLayout.None;

        }

        public Image getBackground()
        {
            return designPnl.BackgroundImage;
        }

        public void clearControl()
        {
            designPnl.Controls.Clear();
            designPnl.Refresh();
            selectControl(null);
        }

        public void addDesignableControl(DesignableControlBase creatingControl)
        {
            addDesignableControl(creatingControl, creatingControl.Location.X, creatingControl.Location.Y);
        }

        public void addDesignableControl(DesignableControlBase creatingControl, int newX, int newY)
        {
            creatingControl.Location = new Point(newX, newY);
            designPnl.Controls.Add(creatingControl);
            this.selectControl(creatingControl);
            this.Refresh();
        }

        public void selectControl(Control ctrl)
        {
            selectedControl = ctrl;
            this.Refresh();
        }

        public void unSelectControl(Control ctrl)
        {
            selectedControl = null;
            this.Refresh();
        }


        public void deleteControl(DesignableControlBase control)
        {
            designPnl.Controls.Remove(control);
            selectControl(null);
        }

        public ControlCollection getAllDesignableControl()
        {
            return designPnl.Controls;
        }
    }
}
